package com.lightheart.sphr.xiaobinggan.bottom.ui

import android.support.v4.view.ViewPager
import android.support.v4.view.ViewPager.OnPageChangeListener
import android.view.Gravity
import android.view.MotionEvent
import android.widget.Toast
import com.gauravk.bubblenavigation.BubbleNavigationLinearView
import com.lightheart.sphr.xiaobinggan.R
import com.lightheart.sphr.xiaobinggan.base.BaseActivity
import com.lightheart.sphr.xiaobinggan.bottom.adapter.ScreenSlidePagerAdapter
import com.lightheart.sphr.xiaobinggan.module.home.presenter.HomePresenter
import kotlinx.android.synthetic.main.activity_timer.*
import java.util.*

class BottomBarActivity : BaseActivity<HomePresenter>() {

    private var timer: Timer? = null
    private var task: TimerTask? = null
    private var minuteTime = 0

    override fun getLayoutId() = R.layout.activity_bottom_bar

    override fun initInjector() {
    }

    override fun initView() {

        val fragList = ArrayList<ScreenSlidePageFragment>()
        fragList.add(ScreenSlidePageFragment.newInstance(getString(R.string.home), R.color.red_inactive))
        fragList.add(ScreenSlidePageFragment.newInstance(getString(R.string.search), R.color.blue_inactive))
        fragList.add(ScreenSlidePageFragment.newInstance(getString(R.string.likes), R.color.blue_grey_inactive))
        fragList.add(ScreenSlidePageFragment.newInstance(getString(R.string.notification), R.color.green_inactive))
        fragList.add(ScreenSlidePageFragment.newInstance(getString(R.string.profile), R.color.purple_inactive))
        val pagerAdapter = ScreenSlidePagerAdapter(fragList, supportFragmentManager)

        val bubbleNavigationLinearView = findViewById<BubbleNavigationLinearView>(R.id.bottom_navigation_view_linear)
//        val fromAsset = Typeface.createFromAsset(this.assets, "rubik.ttf")
//        bubbleNavigationLinearView.setTypeface(fromAsset)
        bubbleNavigationLinearView.setBadgeValue(0, "40")
        bubbleNavigationLinearView.setBadgeValue(1, null) //invisible badge

        bubbleNavigationLinearView.setBadgeValue(2, "7")
        bubbleNavigationLinearView.setBadgeValue(3, "2")
        bubbleNavigationLinearView.setBadgeValue(4, "") //empty badge


        val viewPager = findViewById<ViewPager>(R.id.view_pager)
        viewPager.adapter = pagerAdapter
        viewPager.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(i: Int, v: Float, i1: Int) {}
            override fun onPageSelected(i: Int) {
                bubbleNavigationLinearView.setCurrentActiveItem(i)
            }

            override fun onPageScrollStateChanged(i: Int) {

            }
        })

        bubbleNavigationLinearView.setNavigationChangeListener { view, position ->
            viewPager.setCurrentItem(position, true)
            //底部点击的提示
            showToast("tab$position")
        }

        button.setOnClickListener {
            showToast("点击了按钮")
        }
    }

    private fun showToast(text: String) {
        val toast = Toast.makeText(this@BottomBarActivity, text, Toast.LENGTH_SHORT)
        toast.setGravity(Gravity.CENTER, 0, 0)
        toast.show()
    }

    override fun onResume() {
        super.onResume()
        startTimer()
    }

    private fun initTimer() {//dispatchTouchEvent
        // 初始化计时器
        task = MyTask()
        timer = Timer()
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        when (ev?.action) {
            MotionEvent.ACTION_DOWN ->  //有按下动作时取消定时
                stopTimer()
            MotionEvent.ACTION_UP ->  //抬起时启动定时
                startTimer()
        }
        return super.dispatchTouchEvent(ev)
    }

    inner class MyTask : TimerTask() {
        override fun run() {
            // 初始化计时器
            runOnUiThread {
                minuteTime++
                timeTv.text = minuteTime.toString()
                if (minuteTime == 20) {
                    //在这里弹窗然后停止计时
                    showToast("你去哪里了?")
                    stopTimer()
                }
            }
        }
    }

    private fun startTimer() {
        //启动计时器
        /**
         * java.util.Timer.schedule(TimerTask task, long delay, long period)：
         * 这个方法是说，delay/1000秒后执行task,然后进过period/1000秒再次执行task，
         * 这个用于循环任务，执行无数次，当然，你可以用timer.cancel();取消计时器的执行。
         */
        initTimer()
        try {
            timer?.schedule(task, 1000, 1000)
        } catch (e: IllegalStateException) {
            e.printStackTrace()
            initTimer()
            timer?.schedule(task, 1000, 1000)
        }
    }

    private fun stopTimer() {
        if (timer != null) {
            timer?.cancel()
        }
        minuteTime = 0
        if (timeTv != null) {
            timeTv.text = minuteTime.toString()
        }
    }

    override fun onPause() {
        super.onPause()
        //当activity不在前台是停止定时
        stopTimer()
    }

    override fun onDestroy() {
        //销毁时停止定时
        stopTimer()
        super.onDestroy()
    }
}